package com.study.yangfei.mystudy;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.study.yangfei.mystudy.activity.AlogFibonacciActivity;
import com.study.yangfei.mystudy.activity.AlogGreedyActivity;
import com.study.yangfei.mystudy.activity.AlogQuickSortActivity;
import com.study.yangfei.mystudy.activity.AlogStringDistanceActivity;
import com.study.yangfei.mystudy.activity.DBActivity;
import com.study.yangfei.mystudy.activity.DataBindActivity;
import com.study.yangfei.mystudy.activity.AnimActivity;
import com.study.yangfei.mystudy.activity.JavaOptimizeActivity;
import com.study.yangfei.mystudy.activity.JniActivity;
import com.study.yangfei.mystudy.activity.ListActivity;
import com.study.yangfei.mystudy.activity.ReferrenceActivity;
import com.study.yangfei.mystudy.activity.RxJavaActivity;
import com.study.yangfei.mystudy.activity.ServiceActivity;
import com.study.yangfei.mystudy.activity.StrictModeActivity;
import com.study.yangfei.mystudy.activity.TimingLoggerActivity;

public class MainActivity extends FragmentActivity {

    Class<?> mActivityList [] = {
            AnimActivity.class,
            StrictModeActivity.class,
            TimingLoggerActivity.class,
            ReferrenceActivity.class,
            AlogGreedyActivity.class,
            AlogQuickSortActivity.class,
            AlogStringDistanceActivity.class,
            AlogFibonacciActivity.class,
            DBActivity.class,
            ServiceActivity.class,
            JniActivity.class,
            ListActivity.class,
            JavaOptimizeActivity.class,
            RxJavaActivity.class,
            DataBindActivity.class
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout rlRoot = (LinearLayout) this.findViewById(R.id.activity_main);

        for(Class<?> ca:mActivityList){
            addActivityBtn(rlRoot,ca);
        }

    }


    private void addActivityBtn(LinearLayout rlRoot,final Class<?> cls) {
        Button codeBtn = new Button(this);

        String classFullName = cls.getCanonicalName();
        int lIndex = classFullName.lastIndexOf(".");
        String classShortName = classFullName.substring(lIndex+1);
        classShortName = classShortName.replace("Activity","");

        setBtnAttribute(codeBtn, classShortName, Color.YELLOW, 24);

        codeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(MainActivity.this,cls);
                MainActivity.this.startActivity(in);
            }
        });

        rlRoot.addView(codeBtn);
    }

    private void setBtnAttribute(Button codeBtn, String btnContent, int textColor, int textSize) {
        if (null == codeBtn) {
            return;
        }

        codeBtn.setTextColor(textColor);
        codeBtn.setTextSize((textSize > 16) ? textSize : 24);
        codeBtn.setText(btnContent);
        codeBtn.setAllCaps(false);
        codeBtn.setGravity(Gravity.LEFT);
        codeBtn.setBackground(this.getResources().getDrawable(R.drawable.button_click));

        LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        rlp.setMargins(10,5,10,5);
        codeBtn.setLayoutParams(rlp);
    }
}
